package com.lvmama.dashboard.service.user.impl;

import java.security.NoSuchAlgorithmException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lvmama.com.lvtu.common.utils.MD5;
import com.lvmama.dashboard.dao.user.IUserUserDao;
import com.lvmama.dashboard.entity.UserUser;
import com.lvmama.dashboard.model.page.ResultHandler;
import com.lvmama.dashboard.service.user.IUserUserService;
import com.lvmama.dashboard.utils.ErrDict;

@Service("userUserService")
public class UserUserServiceImpl implements IUserUserService {
	private static final Logger logger = Logger.getLogger(UserUserServiceImpl.class);
	
	@Autowired
	private IUserUserDao userUserDao;

	@Override
	public ResultHandler<UserUser> login(String username, String password) {
		ResultHandler<UserUser> resultHandler = new ResultHandler<UserUser>();
		String encryptPwd = "";
		try {
			encryptPwd = MD5.encode32(password).toUpperCase();
		} catch (NoSuchAlgorithmException e) {
			logger.error(e.getMessage(), e);
			resultHandler.setSuccess(false);
			resultHandler.setException(e);
			resultHandler.setMessage(ErrDict.SYSTEM.TRY_AGAIN_LATER.getMsg());
			return resultHandler;
		}
		UserUser user = userUserDao.queryByUsername(username);
		if(user == null) {
			resultHandler.setSuccess(false);
			resultHandler.setMessage(ErrDict.USER.USERNAME_NOT_EXIST.getMsg());
			return resultHandler;
		}
		if(!encryptPwd.equals(user.getPassword())) {
			resultHandler.setSuccess(false);
			resultHandler.setMessage(ErrDict.USER.PWD_MISTAKE.getMsg());
			return resultHandler;
		}
		resultHandler.setData(user);
		return resultHandler;
	}

	@Override
	public ResultHandler<UserUser> register(UserUser user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultHandler<Boolean> resetPassword(String username, String newPassword, String oldPassword) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultHandler<Boolean> updateUserInfo(UserUser user) {
		// TODO Auto-generated method stub
		return null;
	}
	

}
